import asyncio
from threading import Thread
import websockets
import time

clients = {}

async def echo(websocket, path):
    if path not in clients:
        clients[path] = websocket
        print('insert:', path)
    async for message in websocket:
        await websocket.send(message)

async def main():
    async with websockets.serve(echo, "localhost", 8765):
        await asyncio.Future()  # run forever


thread = Thread(target=lambda: asyncio.run(main()))
thread.start()

time.sleep(3)

async def sendMsg(msg):
    print("in sendMsg", msg, len(clients))
    for _, ws in clients.items():
        await ws.send(msg)

thread2 = Thread(target=lambda: asyncio.run(sendMsg("hello, i am server")))
thread2.start()